using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Multiplicity.Reflection {

    public sealed class StringHeap : StorageHeap<string> {

        public const string Name = "#Strings";

        internal StringHeap(Module module, StorageHeapHeader head, long storageOffset)
            : base(module, head, storageOffset) {

            Throw.ArgumentOutOfRangeExceptionIf(Buffer[BufferOffset] != 0, "First string in '"+Name+"' heap is not empty");
        }

        protected override bool IsValidIndex(uint index) {
            return index < (Header.Size - 1);
        }
        protected override string ReadItem(uint index) {
            int idx = Convert.ToInt32(BufferOffset + index);
            int count = 0;

            while (Buffer[idx + count] != 0)
                count++;

            return Encoding.UTF8.GetString(Buffer, idx, count);
        }
    }

}
